import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_book/constant/app_icon.dart';
import 'package:flutter_book/constant/color.dart';
import 'package:flutter_book/constant/style.dart';
import 'package:flutter_book/data/database/collect_book_helper.dart';
import 'package:flutter_book/data/model/book_model.dart';
import 'package:flutter_book/route/router.dart';
import 'package:flutter_book/util/click_util.dart';
import 'package:flutter_book/widget/common/Image_widget.dart';
import 'package:flutter_book/widget/common/app_icon_button.dart';
import 'package:flutter_book/widget/common/curve_painter.dart';
import 'package:flutter_book/util/extensions/widget_extensions.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';
import 'package:get/get.dart';

class BookDetailPage extends StatelessWidget {
  final BookModel bookModel;
  final BookDetailPageController _controller = BookDetailPageController();

  BookDetailPage(this.bookModel, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light,
      child: GetBuilder<BookDetailPageController>(
        init: _controller,
        initState: (_) async {
          _controller.isCollect.value = await CollectBookHelper.init().isHave(bookModel.id ?? "");
        },
        builder: (ctl) {
          return Scaffold(
            appBar: null,
            body: _getBody(context),
          );
        },
      ),
    );
  }

  Widget _getBody(BuildContext context) {
    return Container(
      color: AppColor.white,
      child: Column(
        children: [
          //顶部背景
          _getTop(context),
          //书名
          Container(
            padding: EdgeInsets.symmetric(horizontal: 16),
            child: Text(
              bookModel.title ?? "",
              style: AppStyle.textMedium18,
              textAlign: TextAlign.center,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
          ),

          SizedBox(height: 16),
          _getInformationUi("作者", bookModel.author ?? ""),
          _getInformationUi("类型", bookModel.cat ?? ""),
          _getInformationUi("来源", "追书神器"),
          _getInformationUi("最新章节", bookModel.lastChapter ?? ""),
          SizedBox(height: 20),

          _getInformationUi("简介", ""),
          SizedBox(height: 4),
          Expanded(
            child: SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 16),
                child: Text(
                  bookModel.shortIntro ?? "",
                  style: AppStyle.textHintRegular18,
                ),
              ),
            ),
          ),
          //底部按钮
          _getBottomBtn(),
        ],
      ),
    );
  }

  //底部按钮
  Widget _getBottomBtn() {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: AppColor.textHint, width: 0.5),
      ),
      height: 50,
      child: Row(
        children: [
          Expanded(
            child: Container(
              color: AppColor.transparent,
              child: Obx(
                () => Text(
                  _controller.isCollect.value ? "移除书架" : "加入书架",
                  style: AppStyle.textMedium18,
                  textAlign: TextAlign.center,
                ),
              ),
            ).onTap(() async {
              if (await CollectBookHelper.init().switchCollect(bookModel)) {
                _controller.isCollect.value = await CollectBookHelper.init().isHave(bookModel.id ?? "");
                "${_controller.isCollect.value ? "收藏成功" : "移除成功"}".toast;
              } else {
                "操作失败".toast;
              }
            }),
          ),
          Container(color: AppColor.textHint, width: 0.5),
          Expanded(
            child: Container(
              height: 50,
              alignment: Alignment.center,
              child: Text(
                "开始阅读",
                style: AppStyle.whiteMedium18,
                textAlign: TextAlign.center,
              ),
              color: AppColor.mainRed,
            ).onTap((){
              if(bookModel.id!=null) {
                AppRouter.toBookPage(bookModel);
              }else{
                "获取数据信息失败".toast;
              }
            }),
          ),
        ],
      ),
    );
  }

  Widget _getInformationUi(String title, String subtitle) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 4),
      child: Row(
        children: [
          Container(
            child: Text(
              title + " : ",
              style: AppStyle.textMedium18,
            ),
          ),
          Expanded(child: Container(
            child: Text(
              subtitle,
              style: AppStyle.textHintRegular18,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ))
        ],
      ),
    );
  }

  Widget _getTop(BuildContext context) {
    return Container(
      height: Get.width / 16.0 * 10.0,
      width: Get.width,
      child: Stack(
        alignment: AlignmentDirectional.topCenter,
        children: [
          //高斯模糊
          Container(
            height: Get.width / 16.0 * 10.0,
            width: Get.width,
            padding: EdgeInsets.only(bottom: 15),
            child: ImageWidget.network(bookModel.cover, isGaussianBlur: true),
          ),
          //底部弧形
          Positioned(
              child: Container(
                height: Get.width / 16.0 * 10.0 / 4,
                width: Get.width,
                child: CustomPaint(
                  foregroundPainter: CurvePainter(value: pi, alpha: 255),
                ),
              ),
              bottom: 0),

          //bar
          _getBar(context),

          //图片
          Positioned(
            child: ImageWidget.network(bookModel.cover, width: 110, height: 159, radius: 4),
            bottom: Get.width / 16.0 * 10.0 / 4 / 5,
          ),
        ],
      ),
    );
  }

  //bar
  Widget _getBar(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: Get.statusBarHeight / Get.pixelRatio),
      child: Row(
        children: [
          AppIconButton(
            iconSize: 28,
            svgName: AppIcon.nav_return,
            buttonWidth: 46,
            buttonHeight: 46,
            iconColor: AppColor.white,
            onTap: () {
              Navigator.pop(context);
            },
          )
        ],
      ),
    );
  }
}


class BookDetailPageController extends GetxController {
  var isCollect = false.obs;
}